package com.wan.fragment


import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

import com.wan.R
import com.wan.activity.WebActivity
import com.wan.adapter.ProjectItemAdapter
import com.wan.base.BaseBean
import com.wan.base.BaseLazyFragment
import com.wan.entity.HomeArtEntity
import com.wan.entity.HomeArtItemEntity
import com.wan.uitls.*
import com.wan.view.ProjectGridItemDecoration
import kotlinx.android.synthetic.main.fragment_project_item.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


/**
 * project 下面的 碎片
 */
class ProjectItemFragment : BaseLazyFragment(), ItemClickListener {

    private val mThreadName = this.javaClass.name


    private var page: Int = 1

    private var cidTag: Int = 0

    private val dataList: ArrayList<HomeArtItemEntity> = ArrayList()

    companion object {
        fun newInstance(bundle: Bundle): ProjectItemFragment {
            var fragment = ProjectItemFragment()
            fragment.arguments = bundle
            return fragment
        }

        const val CID: String = "cid"
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_project_item, container, false)
    }

    override fun initData() {
        var gridLayoutManager = GridLayoutManager(context!!, 2)
        project_page_rv.layoutManager = gridLayoutManager

        val projectItemAdapter = ProjectItemAdapter(context!!, dataList, R.layout.project_page_item_layout)
        projectItemAdapter.setItemClickListener(this)
        project_page_rv.adapter = projectItemAdapter
        val itemDecoration = ProjectGridItemDecoration(context!!)
        itemDecoration.setVerticalDistance(10)
        itemDecoration.setHorizontalDistance(30)
        project_page_rv.addItemDecoration(itemDecoration)
        cidTag = arguments!!.getInt(CID)

        handleNetQuest()
    }


    /**
     * 请求数据
     */
    private fun handleNetQuest() {
        var manager = ApiManager(this)
        val projectItemList = manager.getProjectItemList(page, cidTag)
        projectItemList.enqueue(object : Callback<BaseBean<HomeArtEntity>> {
            override fun onFailure(call: Call<BaseBean<HomeArtEntity>>, t: Throwable) {
                CommonUtils.toastWord(context!!, "${mThreadName}访问失败")
            }

            override fun onResponse(call: Call<BaseBean<HomeArtEntity>>, response: Response<BaseBean<HomeArtEntity>>) {
                CommonUtils.toastWord(context!!, "$mThreadName 获取数据成功")
                var getList = response.body()!!.data.datas
                dataList.addAll(getList)
                project_page_rv.adapter.notifyDataSetChanged()
            }
        })
    }

    override fun onItemClickListener(position: Int) {
        val itemEntity = dataList[position]
        CommonUtils.sendIntent(context!!, WebActivity::class.java, itemEntity.link, itemEntity.title)
    }


    override fun onDestroy() {
        RetrofitHelper.get().cancelCall(this)
        super.onDestroy()
    }

}
